<?php
/***************
* @Project	Warabi
* @Author	Feifei
* @Date		04/10/2009
***************/

/**This module is to deal the dpf login**/

include_once($_SERVER['DOCUMENT_ROOT'].'/header.inc.php');

$httprequest = new wbHttpRequest();

list($headers , $body) = $httprequest->output();

/** a simple way to deal the simple request **/
#var_dump($body);
$xmlparser = simplexml_load_string($body);
if(!$xmlparser){
	header(error_xml_structure);
	exit;
}
$tel = strval($xmlparser->tel);  //get the dpf phone_number_hash
$dev = strval($xmlparser->dev);  //get the dpf device_id
$model = strval($xmlparser->model);  //get the dpf model_name\
if($xmlparser->max[0]){
	$max_arr = $xmlparser->max[0]->attributes();
	list($max_w,$max_h,$max_q) = array($max_arr["w"] , $max_arr["h"] , $max_arr["q"]);//get the dpf max w|h|q
}
if($xmlparser->min[0]){
	$min_arr = $xmlparser->min[0]->attributes(); 
	list($min_w,$min_h,$min_q) = array($min_arr["w"] , $min_arr["h"] , $min_arr["q"]); //get the dpf min w|h|q
}
if($xmlparser->def[0]){
	$def_arr = $xmlparser->def[0]->attributes(); 
	list($def_w,$def_h,$def_q) = array($def_arr["w"] , $def_arr["h"] , $def_arr["q"]); //get the dpf def w|h|q
}

//echo $max_w."<br>".$max_h."<br>".$max_q;
//die;

/** if the tel|dev|model is empty, return a bad request**/
//$telflag = empty($tel)?dpf_teladd_error_tel : "";
//$devflag = empty($dev)?dpf_teladd_error_dev : "";
//$modelflag = empty($model)?dpf_teladd_error_model : "";
//$min_wflag = empty($min_w)?dpf_teladd_error_minw : "";
//$min_hflag = empty($min_h)?dpf_teladd_error_minh : "";
//$def_wflag = empty($def_w)?dpf_teladd_error_defw : "";
//$def_hflag = empty($def_h)?dpf_teladd_error_defh : "";


if(empty($tel)||empty($dev)||empty($model)||empty($min_w)||empty($min_h)||empty($def_w)||empty($def_h)){
	header(error_xml_element_missing);
	exit;
}

$max_q = empty($max_q)?default_quality:$max_q;
$min_q = empty($min_q)?default_quality:$min_q;
$def_q = empty($def_q)?default_quality:$def_q;

list($tel,$dev,$model,$max_w,$max_h,$max_q,$min_w,$min_h,$min_q,$def_w,$def_h,$def_q) = filterValue(array($tel,$dev,$model,$max_w,$max_h,$max_q,$min_w,$min_h,$min_q,$def_w,$def_h,$def_q));

/***insert the user into database 
***or get the passcode from database
**/
$db = new wbDatabase();
$sql = "select * from ".__table_dpf_user__." where tel='".$tel."' and dev='".$dev."' and model='".$model."'";
$set = $db->query($sql);
$passcode = "";

if($db->countResult($set) !=0){
	$tempset = $db->fetchResultBySet($set,MYSQLI_ASSOC);
	$passcode = $tempset["password"];
	$sql = "update ".__table_dpf_user__." set lastlogin=now() where tel='".$tel."' and dev='".$dev."' and model='".$model."'";
	$err = $db->query($sql);
}else{
	/**generate the passcode**/
	$ghd = new Guid();
	$passcode = $ghd->rawOutput();
	$sql = "insert into ".__table_dpf_user__."
				set tel = '".$tel."' ,
					  dev = '".$dev."',
					  model = '".$model."',
					  maxWidth = '".$max_w."',
					  maxHeight = '".$max_h."',
					  maxQuality = '".$max_q."',
					  minWidth = '".$min_w."',
					  minHeight = '".$min_h."',
					  minQuality = '".$min_q."',
					  defWidth = '".$def_w."',
					  defHeight = '".$def_h."',
					  defQuality = '".$def_q."',
					  password = '".$passcode."',
					  lastlogin = now()";
	$err = $db->query($sql);
}

$db->freeResult($set);
$db->disconnect();
if(!$err){
	header(error_server_system);
	exit;
}

/***put the response to the client**/
$tid = getTid();
$resBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$resBody .= "<entry>";
$resBody .= "<pwd>".$passcode."</pwd>";
$resBody .= "<tid>".$tid."</tid>";
$resBody .= "</entry>";

header(success_response);
header('Content-Type: text/xml');
header('Content-Length: '.strlen($resBody));
echo $resBody;

?>